<?php

declare(strict_types=1);

namespace DreamCat\ControllerParamGen4Laravel\Factory;

use Illuminate\Http\Request;
use Laminas\Diactoros\ServerRequestFactory;
use Laminas\Diactoros\StreamFactory;
use Psr\Http\Message\ServerRequestInterface;

/**
 * laravel请求转为psr请求的工厂
 * @author vijay
 */
class LaravelRequestFactory
{
    /**
     * 转换
     * @param Request $request laravel请求
     * @return ServerRequestInterface psr请求对象
     */
    public function create(Request $request): ServerRequestInterface
    {
        $sq = ServerRequestFactory::fromGlobals(
            $request->server(),
            $request->query(),
            $request->post(),
            $request->cookie(),
            $request->file()
        );
        $sq = $sq->withBody((new StreamFactory())->createStream($request->getContent()));
        foreach ($request->attributes->all() as $key => $val) {
            $sq = $sq->withAttribute($key, $val);
        }
        return $sq;
    }
}

# end of file
